<template>
  <div class="app">
        <header-title :city="city" :id="id">随机练习</header-title>

    <my-nav></my-nav>
    <div class="box" @passIndex="recive">
      <div class="pic">
        <img :src="`${pic}`" alt="">
      </div>
      <div class="question">
        <span>{{question}}</span>
      </div>
      <div class="option">
        <ul>
          <li 
          :class="`selection${index}`"
          v-for="(item,index) in optionArr" 
          :key="item"
          :id="Math.pow(2,(index+4))"
          @click="choose(index)"
          >{{item}}</li>
        </ul>
      </div>
      <div class="analyse" v-if="isOpen">
        <p v-html="conciseExplain"></p>
        <p v-html="explain"></p>
      </div>
      <div class="up-box"></div>
    </div>
   <div class="bottom">
      <div class="left">
        <button class="btn-left" @click="sendReduce"><上一题</button>
      </div>
      <div class="mid">
        <div class="current">
          <div class="icon">
            <img src="../assets/current.png" alt />
          </div>
          <span>{{currentNum}}</span>
        </div>
        <div class="wrong">
          <div class="icon">
            <img src="../assets/wrong.png" alt />
          </div>
          <span>{{wrongNum}}</span>
        </div>
        <div class="total" @passIndex="recive">
          <p>
            <span>{{index+1}}</span>
            /{{total}}
          </p>
        </div>
      </div>
      <div class="right">
        <button class="btn-right" @click="sendAdd">下一题></button>
      </div>
    </div>
  </div>
</template>

<script>
import HeaderTitle from "../components/HeaderTitle.vue";
import myNav from "../components/myNav.vue";

export default {
  name: "Random",
  components: { HeaderTitle, myNav },
  mounted() {
    this.city = this.$route.query.city
    this.id = this.$route.query.id
  },
  data() {
    return {
      idArr: [],
      optionArr: [],
      timuId: [],
      answer:'',
      question: "",
      index: 0,
      currentNum: 0,
      wrongNum: 0,
      len: 0,
      isOpen:false,
      conciseExplain:'',
      explain:'',
      pic:"",
      city:'',
      id:''

    };
  },
  methods: {
    getId(i = 0) {
      let arr = []
      fetch(
        "https://apis.netstart.cn/driver/exercise/random?carType=car&course=kemu1&cityCode="+this.id
      )
        .then(response => response.json())
        .then(data => {
          this.timuId.push(data);
          this.idArr = JSON.parse(JSON.stringify(this.timuId[0])).questionIds;
          // console.log(this.idArr);
          fetch("https://apis.netstart.cn/driver/question?ids=" + this.idArr[i])
            .then(response => response.json())

            .then(response => {
              // console.log(response);
              this.question = response[0].question;
              arr = [response[0].optionA,response[0].optionB,response[0].optionC,response[0].optionD,response[0].optionE,response[0].optionF,response[0].optionG,response[0].optionH]
              // console.log(this.optionArr);
              arr.forEach(item => {
                if(item.length != 0 ){
                  // console.log(item);
                  this.optionArr.push(item)
                  
                }
              });
              this.answer = response[0].answer
              this.conciseExplain = response[0].conciseExplain
              this.explain = response[0].explain
              this.pic = response[0].mediaContent

              
            });
        });
    },

    sendAdd() {
      this.len = this.idArr.length;

      this.index++;
      if (this.index >= this.len) {
        this.index = this.len - 1;
      }
      this.getId(this.index);
      this.optionArr =[]
      this.isOpen = false

      this.$emit("passIndex", this.index);
    },
    sendReduce() {
      this.index--;
      if (this.index <= 0) {
        this.index = 0;
      }
      this.getId(this.index);

      this.optionArr =[]
      this.isOpen = false

      this.$emit("passIndex", this.index);
    },
    recive(num) {
      this.index = num;
    },
    choose(index) {
      const selection = document.querySelector(`.selection${index}`);
      let id = selection.getAttribute("id")
      let el = document.getElementById(this.answer)
      // console.log(id);
      if (id == this.answer) {
        selection.style.backgroundColor = "#16a816";
        if(this.currentNum + this.wrongNum >= this.index+1) {
          return this.currentNum
        }else {
        this.currentNum++;

        }
      } else {
        selection.style.backgroundColor = "#d81e06";
        el.style.backgroundColor = "#16a816"
        if(this.currentNum + this.wrongNum >= this.index+1) {
          return this.wrongNum
        }else {
        this.wrongNum++;

        }
        this.isOpen = true
      }
    }
  },
  computed: {
    total: {
      get() {
        let total = this.idArr.length;
        return total;
      }
    }
  },
  created() {
    this.getId();
  }
};
</script>
<style lang="scss" scoped>
.app {
  width: 100%;
  .box {
    width: 100%;
    margin-top: 20px;
    .pic {
      width: 100%;
      text-align: center;
      img {
        width: 100%;
      }
    }
    .question {
      width: 100%;
      span {
        padding-left: 15px;
        padding-right: 10px;
        font-size: 19px;
      }
    }
    .option {
      width: 100%;
      margin-top: 20px;
      ul {
        width: 100%;
        li {
          width: 90%;
          margin: 0 auto;
          background-color: rgba(204, 204, 204, 0.3);
          margin-bottom: 10px;
          padding: 20px 0;
          padding-left: 10px;
          border-radius: 20px;
        }
      }
    }
    .analyse {
      width: 100%;
      background-color: rgba(204, 204, 204, 0.3);
      margin-top: 20px;
      p {
        padding-left: 10px;
        font-size: 24px;
        color: #666;
      }
    }
    .up-box {
      width: 100px;
      height: 50px;
    }
  }
.bottom {
    width: 100%;
    height: 50px;
    background-color: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    display: flex;
    line-height: 50px;
    z-index: 10000;
    .left,
    .right {
      flex-grow: 1;
      text-align: center;
      button {
        border: 0;
        background-color: transparent;
      }
    }
    .mid {
      flex-grow: 4;
      display: flex;
      .current,
      .wrong,
      .total {
        flex-grow: 1;
        text-align: center;
        display: flex;
        align-items: center;
        justify-content: center;
        .icon {
          width: 30px;
          height: 40px;
          line-height: 40px;
          img {
            width: 20px;
            height: 20px;
            vertical-align: middle;
          }
          span {
            font-size: 18px;
          }
        }
      }
    }
  }
  
}
</style>